/*
 ============================================================================
 @file       : Main.cpp
 @author     : Jonathan MERCIER
 @date       : 31 janv. 2010
 @version    : 0.1
 
 @section LICENSE
 Copyright   : GPL v3+
 (c)Jonathan MERCIER (read doc for more information)
 Copyright (C) Author: Jonathan MERCIER 2010

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

  
 @section DESCRIPTION
 Description : 
 ============================================================================
 */

#include "Main.h"


int main(int argc,char **argv)
{
	bool 			isServer 	= false;
	std::wstring 	serverOption=L"--server";
	std::wstring	option		=L"";
	/**
	 * Parse command line
	 */
	for (unsigned short i=1; i<argc; i++)
	{
		option = widen(argv[i]);

		if ( serverOption.compare(option) )
		{
			isServer = true;
		}
		else
		{
			std::wcout << L"Unrecognized option: " << option << std::endl;
		}
	}

	if (isServer)
	{
		engine::Network network;
		engine::Game	game;

		Application application(&network, &game);
		application.run();
	}
	else
	{
		engine::Network 	network;
		engine::Game		game;
		engine::Graphics	graphics;
		engine::Sound		sound;

		Application application(&network, &game, &graphics, &sound);
		application.run();

	}
	return 0;
}

std::string narrow(const std::wstring& ws)
{
	std::vector<char> buffer(ws.size());
	std::locale loc("english");
	std::use_facet< std::ctype<wchar_t> >(loc).narrow(ws.data(), ws.data() + ws.size(), '?', &buffer[0]);

    return std::string(&buffer[0], buffer.size());
}

std::wstring widen(const std::string& s)
{
	std::vector<wchar_t> buffer(s.size());
	std::locale loc("english");
	std::use_facet< std::ctype<wchar_t> >(loc).widen(s.data(), s.data() + s.size(), &buffer[0]);

    return std::wstring(&buffer[0], buffer.size());
}
